package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RightsMapper;
import com.jt.mapper.RoleMapper;
import com.jt.mapper.RoleRightsMapper;
import com.jt.pojo.Rights;
import com.jt.pojo.Role;
import com.jt.pojo.RoleRights;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements  RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleRightsMapper roleRightsMapper;


    @Autowired
    private RightsMapper rightsMapper;


    @Autowired
     private  RightsService  rightsService;


    @Override
    public List<Role> getRoleList() {
        //1.查询所有的role角色信息
        List<Role> roleList = roleMapper.selectList(null);
        //2.遍历获取关联的角色信息
        for (Role role : roleList){
            int roleId = role.getRoleId();
            //获取所有的关联权限Id
            List<Integer> rightsIds = roleMapper.findRightsIdList(roleId);
            if(rightsIds == null || rightsIds.size()==0) break;

            //将rightsId封装为3级权限菜单
            List<Rights> rightsList = rightsService.getRightsListByIds(rightsIds);
            role.setRightsList(rightsList);
        }
        return roleList;
    }



    @Override
    @Transactional
    public void addRole(Role role) {
        roleMapper.insert(role);
    }

    @Override
    @Transactional
    public void saveRights(Integer roleId, Integer[] rightsIds) {

        RoleRights roleRights=new RoleRights();
        for (Integer rightId:rightsIds) {
            roleRights.setRoleId(roleId).setRightsId(rightId);
            roleRightsMapper.insert(roleRights);
        }
    }



    @Override
    @Transactional
    public Role deleteRoleRights(Integer roleId, Integer rightsId, Integer level) {
        QueryWrapper<RoleRights> queryWrapper=new QueryWrapper<>();
        if(level==3)
        {
            queryWrapper.eq("role_id", roleId).eq("rights_id", rightsId);
            roleRightsMapper.delete(queryWrapper);
        }
        else  if(level==2)
        {
            List<Integer> rightsIds=rightsService.getRightsChildrenList(rightsId);
            rightsIds.add(rightsId);
            roleMapper.deleteRoleRights(roleId, rightsIds);
        }
        else if(level == 1)
        {
            List<Integer> rightsIds=new ArrayList<>();
            List<Integer> twoIdList=rightsService.getRightsChildrenList(rightsId);
            for(Integer twoRightsId:twoIdList)
            {
                List<Integer> threeIdList=rightsService.getRightsChildrenList(twoRightsId);
                for(Integer threeId:threeIdList)
                {
                    rightsIds.add(threeId);
                }
                rightsIds.add(twoRightsId);
            }
            rightsIds.add(rightsId);
            roleMapper.deleteRoleRights(roleId, rightsIds);
        }
        return  findRoleRightsListById(roleId);
    }

    @Override
    public Role findRoleRightsListById(Integer roleId) {
        Role role=roleMapper.selectById(roleId);
        //获取所有的关联权限ID
        List<Integer> rightIds=roleMapper.findRightsIdList(roleId);
        if(rightIds == null || rightIds.size()==0)
        {
            return role;
        }
        //将roghtsIds封装为3级权限菜单
        List<Rights> rightsList=rightsService.getRightsListByIds(rightIds);
        role.setRightsList(rightsList);
        return role;
    }

    @Override
    public Role getRole(Integer roleId) {
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        roleMapper.selectOne(queryWrapper);
        return  roleMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional
    public void upRole(Role role) {
        roleMapper.updateById(role);
    }

    /**
     * 删除角色相关信息的同时也要删除库中的权限信息
     * @param roleId
     */
    @Override
    @Transactional
    public void deleteRoleById(Integer roleId) {
         roleMapper.deleteById(roleId);
         roleMapper.deleteRoleRights(roleId, roleMapper.findRightsIdList(roleId));
    }


}
